home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / mdsmax < prev    next >
Text File  |  1995-11-20  |  7KB  |  215 lines

  1. #
  2. #MDSMAX  [x, fmax, nf] = MDSMAX(fun, x0, STOPIT, SAVIT) attempts to
  3. #        maximize the function specified by the string fun, using the
  4. #        starting vector x0.  The method of multi-directional search is used.
  5. #        Output arguments:
  6. #               x    = vector yielding largest function value found,
  7. #               fmax = function value at x,
  8. #               nf   = number of function evaluations.
  9. #        The iteration is terminated when either
  10. #               - the relative size of the simplex is <= STOPIT(1)
  11. #                 (default 1e-3),
  12. #               - STOPIT(2) function evaluations have been performed
  13. #                 (default inf, i.e., no limit), or
  14. #               - a function value equals or exceeds STOPIT(3)
  15. #                 (default inf, i.e., no test on function values).
  16. #        The form of the initial simplex is determined by STOPIT(4):
  17. #           STOPIT(4) = 0: regular simplex (sides of equal length, the default)
  18. #           STOPIT(4) = 1: right-angled simplex.
  19. #        Progress of the iteration is not shown if STOPIT(5) = 0 (default 1).
  20. #        If a non-empty fourth parameter string SAVIT is present, then
  21. #        `SAVE SAVIT x fmax nf' is executed after each inner iteration.
  22. #        NB: x0 can be a matrix.  In the output argument, in SAVIT saves,
  23. #            and in function calls, x has the same shape as x0.
  24.  
  25. # This implementation uses 2n elements of storage (two simplices), where x0
  26. # is an n-vector.  It is based on the algorithm statement in [2, sec.3],
  27. # modified so as to halve the storage (with a slight loss in readability).
  28.  
  29. # References:
  30. # [1] V.J. Torczon, Multi-directional search: A direct search algorithm for
  31. #     parallel machines, Ph.D. Thesis, Rice University, Houston, Texas, 1989.
  32. # [2] V.J. Torczon, On the convergence of the multi-directional search
  33. #     algorithm, SIAM J. Optimization, 1 (1991), pp. 123-145.
  34. # [3] N.J. Higham, Optimization by direct search in matrix computations,
  35. #     Numerical Analysis Report No. 197, University of Manchester, UK, 1991;
  36. #     to appear in SIAM J. Matrix Anal. Appl, 14 (2), April 1993.
  37.  
  38. # By Nick Higham, Department of Mathematics, University of Manchester, UK.
  39. #                 na.nhigham@na-net.ornl.gov
  40. # July 27, 1991.
  41.  
  42. #
  43. # Translated to RLaB, Ian Searle
  44. # Febuary 1994.
  45. #
  46.  
  47. # Dependencies
  48. require rem
  49.  
  50. mdsmax = function (fun, X, stopit, savit)
  51. {
  52.   global (eps)
  53.  
  54.   x = X;    # Copy input
  55.   n = prod(size(x));
  56.   x0 = x[:];    # Work with column vector internally.
  57.  
  58.   mu = 2;      # Expansion factor.
  59.   theta = 0.5; # Contraction factor.
  60.  
  61.   # Set up convergence parameters etc.
  62.   if (!exist(stopit)) { stopit[1] = 1e-3; }
  63.   tol = stopit[1];  # Tolerance for cgce test based on relative size of simplex.
  64.   if (max(size(stopit)) == 1) { stopit[2] = inf(); }    # Max no. of f-evaluations.
  65.   if (max(size(stopit)) == 2) { stopit[3] = inf(); }    # Default target for f-values.
  66.   if (max(size(stopit)) == 3) { stopit[4] = 0; }    # Default initial simplex.
  67.   if (max(size(stopit)) == 4) { stopit[5] = 1; }    # Default: show progress.
  68.   trace  = stopit[5];
  69.   if (!exist(savit)) { savit = []; }            # File name for snapshots.
  70.  
  71.   V = [zeros(n,1), eye(n,n)]; T = V;
  72.   f = zeros(n+1,1); ft = f;
  73.   V[;1] = x0; 
  74.   x = reshape (x0, x.nr, x.nc);
  75.   f[1] = fun (x);
  76.   fmax_old = f[1];
  77.  
  78.   if (trace) { printf("f(x0) = %9.4e\n", f[1]); }
  79.  
  80.   k = 0; m = 0;
  81.  
  82.   # Set up initial simplex.
  83.   scale = max([norm(x0,"i"),1]);
  84.   if (stopit[4] == 0)
  85.   {
  86.     # Regular simplex - all edges have same length.
  87.     # Generated from construction given in reference [18, pp. 80-81] of [1].
  88.     alpha = scale / (n*sqrt(2)) * [ sqrt(n+1)-1+n, sqrt(n+1)-1 ];
  89.     V[;2:n+1] = (x0 + alpha[2]*ones(n,1)) * ones(1,n);
  90.     for (j in 2:n+1)
  91.     {
  92.       V[j-1;j] = x0[j-1] + alpha[1];
  93.       x = reshape (V[;j], x.nr, x.nc);
  94.       f[j] = fun (x);
  95.     }
  96.   else
  97.     # Right-angled simplex based on co-ordinate axes.
  98.     alpha = scale*ones(n+1,1);
  99.     for (j in 2:n+1)
  100.     {
  101.       V[;j] = x0 + alpha[j]*V[;j];
  102.       x = reshape (V[;j], x.nr, x.nc);
  103.       f[j] = fun (x);
  104.     }
  105.   }
  106.   nf = n+1;
  107.   msize = 0;        # Integer that keeps track of expansions/contractions.
  108.   flag_break = 0;    # Flag which becomes true when ready to quit outer loop.
  109.  
  110.   while (1)    ###### Outer loop.
  111.   {
  112.     k = k+1;
  113.  
  114.     # Find a new best vertex  x  and function value  fmax = f(x).
  115.     fmax = max (f); j = maxi (f);
  116.     V[;1,j] = V[;j,1]; 
  117.     v1 = V[;1];
  118.     if (!isempty(savit)) { x = reshape(v1, x,nr, x.nc); write ("savit", x,fmax,nf); }
  119.     f[1,j] = f[j,1];
  120.     if (trace)
  121.     {
  122.       printf("Iter. %2.0f,  inner = %2.0f,  size = %2.0f,  ", k, m, msize);
  123.       printf("nf = %3.0f,  f = %9.4e  (%2.1f)\n", nf, fmax, ...
  124.              100*(fmax-fmax_old)/(abs(fmax_old)+eps));
  125.     }
  126.     fmax_old = fmax;
  127.  
  128.     # Stopping Test 1 - f reached target value?
  129.     if (fmax >= stopit[3])
  130.     {
  131.       msg = "Exceeded target...quitting\n";
  132.       break  # Quit.
  133.     }
  134.  
  135.     m = 0;
  136.     while (1)   ### Inner repeat loop.
  137.     {
  138.       m = m+1;
  139.   
  140.       # Stopping Test 2 - too many f-evals?
  141.       if (nf >= stopit[2])
  142.       {
  143.         msg = "Max no. of function evaluations exceeded...quitting\n";
  144.         flag_break = 1; 
  145.         break  # Quit.
  146.       }
  147.   
  148.       # Stopping Test 3 - converged?   This is test (4.3) in [1].
  149.       size_simplex = norm(V[;2:n+1] - v1[;ones(1,n)],"1") / max([1, norm(v1,"1")]);
  150.       if (size_simplex <= tol)
  151.       {
  152.         sprintf(msg, "Simplex size %9.4e <= %9.4e...quitting\n", ...
  153.                 size_simplex, tol);
  154.         flag_break = 1; 
  155.         break  # Quit.
  156.       }
  157.   
  158.       for (j in 2:n+1)      # ---Rotation (reflection) step.
  159.       {
  160.         T[;j] = 2*v1 - V[;j];
  161.         x = reshape (T[;j], x.nr, x.nc);
  162.         ft[j] = fun (x);
  163.       }
  164.       nf = nf + n;
  165.   
  166.       replaced = ( max(ft[2:n+1]) > fmax );
  167.   
  168.       if (replaced)
  169.       {
  170.         for (j in 2:n+1)    # ---Expansion step.
  171.         {
  172.           V[;j] = (1-mu)*v1 + mu*T[;j];
  173.           x = reshape (V[;j], x.nr, x.nc);
  174.           f[j] = fun (x);
  175.         }
  176.         nf = nf + n;
  177.         # Accept expansion or rotation?
  178.         if (max(ft[2:n+1]) > max(f[2:n+1]))
  179.         {
  180.           V[;2:n+1] = T[;2:n+1];
  181.           f[2:n+1] = ft[2:n+1];    # Accept rotation.
  182.         else
  183.           msize = msize + 1;    # Accept expansion (f and V already set).
  184.         }
  185.       else
  186.         for (j in 2:n+1)    # ---Contraction step.
  187.         {
  188.           V[;j] = (1+theta)*v1 - theta*T[;j];
  189.           x = reshape (V[;j], x.nr, x.nc);
  190.           f[j] = fun (x);
  191.         }
  192.         nf = nf + n;
  193.         replaced = ( max(f[2:n+1]) > fmax );
  194.         # Accept contraction (f and V already set).
  195.         msize = msize - 1;
  196.       }
  197.   
  198.       if (replaced) { break }
  199.       if (trace && rem(m,10) == 0) 
  200.       { 
  201.         printf("        ...inner = %2.0f...\n",m);
  202.       }
  203.     }        ### Of inner repeat loop.
  204.  
  205.     if (flag_break) { break }
  206.  
  207.   }    ###### Of outer loop.
  208.  
  209.   # Finished.
  210.   if (trace) { printf(msg); }
  211.   x = reshape (v1, x.nr, x.nc);
  212.  
  213.   return << x = x; fmax = fmax; nf = nf>>;
  214. };
  215.